<?php
defined('BASEPATH') or exit('No direct script access allowed');


class Business extends MY_Controller
{
    private $appKey = 'c3ea4acb181da5c4f9a46c66fce0658c';    //  你的Key
    private $appScret = '16c879fac6584b10889647cf7ee591e1';   //  你的Secret
    private $app_token_json = '{}'; //  第一次需要手动授权获取京东Token然后粘贴到这里

    public function __construct()
    {
        parent::__construct();
        if ($this->userinfo['group'] != 1 && $this->userinfo['group'] != 2) {
            $this->groupjunp($this->userinfo['group']);
        }
        $field = $_SERVER['REMOTE_ADDR'];

        $this->host = 'pdd';

        $this->address2 = '流快递更新时间：早上11点、晚上10点，当天的快递没出会在次日上午11点更新！！';

    }


//主页
    public function all()
    {
        $this->address = '测试页面';
        $this->load->view('business/new');
    }

    /**
     * 获取用户佣金配置信息
     */
    public function getcommissionbyuser()
    {
        $commission['1'] = $this->getcommissionconfig($this->project, $this->user_id, 1);
        $commission['2'] = $this->getcommissionconfig($this->project, $this->user_id, 2);
        $commission['3'] = $this->getcommissionconfig($this->project, $this->user_id, 3);
        $commission['4'] = $this->getcommissionconfig($this->project, $this->user_id, 4);
        return $commission;
    }


    //主页
    public function index()
    {
        redirect(base_url() . '/seller');
        exit;
        $this->load->model('Jd_users_model');
        $this->address = '商户首页';
        $data['link'] = $this->SetExtensionLink($this->user_id);
        $this->load->view('business/head');
        $this->load->view('business/left');
        $redis = $this->MyRedis();
        $msg = $redis->get(date('Y-m-d'));
        if (!$msg) {
            $msg = rand(7500, 8500) / 100;
            $msg = $msg . "%";
            $redis->setex(date('Y-m-d'), 24 * 3600, $msg);
        }
        $data['msg'] = $msg;
        $commission = $this->getcommissionbyuser();
        $list = [];
        $min1 = 0;
        $min2 = 0;
        $min3 = 0;
        $min4 = 0;
        for ($i = 0; $i < 8; $i++) {
            $row = [];
            foreach ($commission as $key => $vlaue) {
                $money = $vlaue['money'][$i] . "-" . $vlaue['money'][$i + 1];
                $gold = $vlaue['gold'][$i];

                $row[] = $money;
                $row[] = $gold;
            }
            $list[$i] = $row;
        }


        $data['list'] = $list;
        $data['commission'] = $commission;
        $data['banner'] = $this->getbanner();
        $this->load->view('business/index', $data);

    }


    public function changeSales()
    {
        $this->load->view('business/head');
        $this->load->view('business/left');
        $this->load->view('business/changeSales');
    }

    public function getbanner()
    {

        $this->load->model('Jd_banner_model');
        $list = $this->Jd_banner_model->getlist();


        return $list;
    }


    public function getindexdata()
    {
        $this->load->model('Jd_user_total_model');
        $a = $this->Jd_user_total_model->getonebyuserid($this->user_id);

        $a = '';
        if (empty($a)) {
            $a['task_nub'] = 0;
            $a['extension'] = 0;
            $a['extension_nub'] = 0;
        } else {
            $a = $a[0];
        }
        $a['money'] = $this->userinfo['money'];
        $row['list'][0] = $a;

        $this->sendjsonmsg($row);
    }


    /**
     * 商户充值页面
     */
    public function pay()
    {
        $this->load->library('parser');
        $this->address = '充值';
        $this->load->view('business/head');
        $this->load->view('business/left');

        if ($this->host == "jd") {
            $this->load->view('business/jdddlb');
        } else {
            $this->load->view('business/ddlb');
        }
    }


    /**
     * 消费记录列表
     */
    public function paylog()
    {
        $this->address = '消费记录';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $this->load->view('business/paylog');
    }


    /**
     * 获取充值记录
     */
    public function rechargelog()
    {
        $this->load->library('parser');
        $this->address = '充值记录';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $this->load->view('business/rechargelog');

    }

    public function express_list($taskid = "")
    {
        $this->load->library('parser');
        $this->address = '充值记录';
        $this->load->view('business/head');
        $this->load->view('business/left');

        $data['taskid'] = $taskid;
        $this->load->view('business/express_list', $data);
    }


    public function course($taskid = "")
    {
        $this->load->library('parser');
        $this->address = '任务教程';
        $this->load->view('business/head');
        $this->load->view('business/left');


        $this->load->view('business/course');
    }


    /**
     * 获取资金详细列表
     */
    public function getpaylog()
    {
        $page = $this->input->post('page');
        $epage = $this->input->post('epage');
        if (empty($epage)) {
            $epage = 10;
        }
        $this->load->model('Jd_pay_log_model');


        $date1 = $this->input->post('date1') ? $this->input->post('date1') : "";
        $date2 = $this->input->post('date2') ? $this->input->post('date2') : "";
        $where = "  user_id =$this->user_id";
        if ($date1) {
            $date1 = strtotime(date('Y-m-d 00:00:00', strtotime($date1)));
            $where .= " AND addtime>$date1 ";
        }
        if ($date2) {
            $date2 = strtotime(date('Y-m-d 23:59:59', strtotime($date2)));
            $where .= " AND addtime<$date2 ";
        }


        $order['id'] = "desc";
        $a = $this->Jd_pay_log_model->pageData($where, "*", $page, $epage, $order);
        $a['epage'] = $epage;
        $a['page'] = $page;


        foreach ($a['list'] as $id => $v) {
            $a['list'][$id]['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
            switch ($v['type']) {
                case 1 :
                    $a['list'][$id]['type'] = '充值';
                    break;
                case 2 :
                    $a['list'][$id]['type'] = '发布任务';
                    break;
                case 3 :
                    $a['list'][$id]['type'] = '推荐奖励';
                    break;
                case 4 :
                    $a['list'][$id]['type'] = '测试员刷单';
                    break;
                case 5 :
                    $a['list'][$id]['type'] = '兑换金币';
                    break;
                case 6 :
                    $a['list'][$id]['type'] = '后台调整';
                    break;
            }
        }


        $this->sendjsonmsg($a);

    }


    public function getgold()
    {
        $this->address = '金币兑换';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $this->load->view('business/getgold');
    }


    /**
     * 兑换金币
     */
    public function getgoldaction()
    {
        $money = $this->input->post('money');
        $gold = $money * 10 / 9;
        $userinfo = $this->getcuserinfo(1);

        $this->load->model('Jd_task_jd_model');

        $hassend = $this->Jd_task_jd_model->getmytimetask($this->user_id);
        $hassendmoney = $hassend['money'];
        if ($userinfo['money'] < $money + $hassendmoney) {
            if ($userinfo['money'] < $money) {
                $msg['code'] = "1";
                $msg['msg'] = "余额不足";
                $this->sendjsonmsg($msg);
            } else {
                $msg['code'] = "1";
                $msg['msg'] = "有定时任务所以余额不足";
                $this->sendjsonmsg($msg);
            }
        }
        if ($money > 1000) {
            $msg['code'] = "1";
            $msg['msg'] = "单次只提供1000元以下兑换";
            $this->sendjsonmsg($msg);
        }
        $row['money'] = $money;
        $row['gold'] = round($gold, 2);
        $row['user_id'] = $this->user_id;
        $r = $this->moneyupdate($row, 5);
        if ($r) {
            $msg['code'] = 0;
            $msg['msg'] = '兑换成功';
            // $this->ResetUserinfo();
            $this->sendjsonmsg($msg);
        }
    }

    /**
     * 商户添加充值动作
     */
    public function payaction()
    {

        $account = $this->input->post('account');
        $username = $this->input->post('username');
        $row['pic'] = $this->input->post('pic') ? $this->input->post('pic') : '';
        $money = $this->input->post('money');
        $money = abs($money);

        $this->load->model('Jd_pay_model');
        if (!empty($row['pic'])) {
            $pic = $this->movepic($row['pic']);
            if (!$pic) {
                $msg['code'] = 1;
                $msg['msg'] = '图片上传失败';
                $this->sendjsonmsg($msg);
            }
            $newrow = array();
            $newrow['user_id'] = $this->user_id;
            $newrow['address'] = $pic['path'];
            $newrow['url'] = $pic['url'];
            $newrow['addtime'] = time();
            $this->load->model('Jd_pic_model');
            $result = $this->Jd_pic_model->addpic($newrow);

            if (!$result) {
                $msg['code'] = 1;
                $msg['msg'] = '图片上传失败';
                $this->sendjsonmsg($msg);
            }
            $row['pic'] = $result;
            $row['picurl'] = $pic['url'];
        }
        $row['account'] = '6213903301273483';
        $row['money_maker'] = $username;
        $row['money'] = $money;
        $result = $this->Jd_pay_model->addpay($row);
        if (!$result) {
            $msg['code'] = 1;
            $msg['msg'] = '新增失败';
            $this->sendjsonmsg($msg);
        }
        $this->sendjsonmsg();
    }


    /**
     * 获取充值列表
     */
    public function getpaylist()
    {
        $page = $this->input->post('page');
        $epage = $this->input->post('epage');
        if (empty($epage)) {
            $epage = 10;
        }
        $this->load->model('Jd_pay_model');

        $order['id'] = "desc";

        $date1 = $this->input->post('date1') ? $this->input->post('date1') : "";
        $date2 = $this->input->post('date2') ? $this->input->post('date2') : "";
        $where = "  user_id =$this->user_id";
        if ($date1) {
            $date1 = strtotime(date('Y-m-d 00:00:00', strtotime($date1)));
            $where .= " AND addtime>$date1 ";
        }
        if ($date2) {
            $date2 = strtotime(date('Y-m-d 23:59:59', strtotime($date2)));
            $where .= " AND addtime<$date2 ";
        }

        $a = $this->Jd_pay_model->pageData($where, "id,addtime,money,money_maker,status,remark", $page, $epage, $order);
        foreach ($a['list'] as $id => $v) {
            $a['list'][$id]['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
            switch ($v['status']) {
                case 0 :
                    $a['list'][$id]['status'] = '未审核';
                    break;
                case 1 :
                    $a['list'][$id]['status'] = '已通过';
                    break;
                case 2 :
                    $a['list'][$id]['status'] = '被拒绝';
                    break;
            }

        }
        $a['epage'] = $epage;
        $a['page'] = $page;
        $this->sendjsonmsg($a);
    }


    /**
     * 京东任务添加页面
     */
    public function jdtask()
    {
        if ($this->host == 'pdd') {
            echo "<script>history.go(-1); </script>";
            exit;
        }
        $this->load->library('parser');
        $this->address = '任务发布';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $this->load->view('business/jdtask');
    }


    public function confirm()
    {
        $this->load->model('Jd_users_model');
        $shoplist = $this->Jd_users_model->getUserShop($this->user_id);
        $data['shoplist'] = $shoplist;

        $this->load->helper(array('form', 'url'));
        $this->load->view('business/head');
        $this->load->view('business/left');
        $this->load->view('business/confirm.vue', $data);
    }


    public function pddtask()
    {
        if ($this->host == 'jd') {
            echo "<script>history.go(-1); </script>";
            exit;
        }
        $this->load->library('parser');
        $this->address = '拼多多任务发布';
        $type_h_number = $this->getconfig("type_h_number");
        $data['min'] = $type_h_number['min'];
        $data['max'] = $type_h_number['max'];
        $data['is12'] = 0;
        if (time() > strtotime(date('Y-m-d 20:00:00'))) {
            $data['is12'] = 1;
        }


        $this->load->model('Jd_users_model');
        $shoplist = $this->Jd_users_model->getUserShop($this->user_id);
        $data['shoplist'] = $shoplist;

        $this->load->helper(array('form', 'url'));
        $this->load->view('business/head');
        $this->load->view('business/left');
        $this->load->view('business/pddtask', $data);
    }


    /**
     * 京东任务管理
     */
    public function jdtasklist()
    {
        $this->load->library('parser');
        $this->address = '任务管理';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $this->load->view('business/jdtasklist');
    }


    /**
     * 查看任务
     */
    public function viewjdtask($id)
    {
        $this->address = '查看任务';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $row = array();
        $this->load->model('Jd_task_jd_model');
        $row = $this->Jd_task_jd_model->getonebyid($id);
        $str = explode("/", $row['picurl']);
        if (isset($str[1]) && $str[1] == 'pic') {
            $row['picurl'] = base_url() . "public/uploads" . $row['picurl'];
        }
        $this->load->view('business/viewjdtask', $row);
    }

    public function viewpddtask($id)
    {
        $this->address = '查看任务';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $row = array();
        $this->load->model('Jd_task_jd_model');

        $row = $this->Jd_task_jd_model->getonebyid($id);
        $str = explode("/", $row['picurl']);
        if (isset($str[1]) && $str[1] == 'pic') {
            $row['picurl'] = base_url() . "public/uploads" . $row['picurl'];
        }
        $this->load->view('business/viewpddtask', $row);
    }


    public function relodjdtask($id)
    {
        $this->address = '重新发布任务';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $row = array();
        $this->load->model('Jd_task_jd_model');
        $row = $this->Jd_task_jd_model->getonebyid($id);
        if ($row['business_id'] != $this->user_id) {
            echo "<script>history.go(-1); </script>";
        }
        $str = explode("/", $row['picurl']);
        if (isset($str[1]) && $str[1] == 'pic') {
            $row['picurl'] = base_url() . "public/uploads" . $row['picurl'];
        }
        $this->load->view('business/relodjdtask', $row);
    }


    /**
     * 重新发布任务
     */
    public function againtask()
    {
        $id = $this->input->post('id');
        $this->load->model('Jd_task_jd_model');
        $row = $this->Jd_task_jd_model->getonebyid($id);
        $array[] = 'link';
        $array[] = 'goods_name';
        $array[] = 'price';
        $array[] = 'pay_price';
        $array[] = 'number';
        $array[] = 'each_single';
        $array[] = 'keyword';
        $array[] = 'coupon';
        $array[] = 'sku';
        $array[] = 'remarks';
        $array[] = 'shop_name';
        foreach ($row as $id => $v) {
            if (!in_array($id, $array)) {
                unset($row[$id]);
            }
        }
        $r = $this->SendJdTask($row);
    }


    public function GetPDDTaskList()
    {
        $this->GetJdTaskList(2);
    }

    public function GetJdTaskList($type = 1)
    {
        $page = $this->input->post('page');
        $epage = $this->input->post('epage');
        if (empty($epage)) {
            $epage = 10;
        }
        $this->load->model('Jd_task_jd_model');

        $date1 = $this->input->post('date1') ? $this->input->post('date1') : "";
        $date2 = $this->input->post('date2') ? $this->input->post('date2') : "";
        $where = "business_id=$this->user_id AND type=$type";

        if ($date1) {
            $date1 = strtotime(date('Y-m-d 00:00:00', strtotime($date1)));
            $where .= " AND addtime>$date1 ";
        }
        if ($date2) {
            $date2 = strtotime(date('Y-m-d 23:59:59', strtotime($date2)));
            $where .= " AND addtime<$date2 ";
        }
        $order['id'] = "DESC";
        $select = "id,number,finish_nub,pay_type,pay_price,each_single,gold,group_price,status,goods_name,single_price,keyword,addtime,sku,releasetime";
        $a = $this->Jd_task_jd_model->pageData($where, $select, $page, $epage, $order);

        // echo $this->db->last_query();die;

        $a['epage'] = $epage;
        $a['page'] = $page;

        foreach ($a['list'] as $id => $v) {
            $a['list'][$id]['money'] = round($v['pay_price'] * $v['number'] * $v['each_single'], 2);
            //$a['list'][$id]['gold']= $this->getcommission( $a['list'][$id]['money'],$this->user_id);
            $a['list'][$id]['number'] = $v['finish_nub'] . "/" . $v['number'];
            $a['list'][$id]['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
            $a['list'][$id]['releasetime'] = $v['releasetime'] ? date('Y-m-d H:i:s', $v['releasetime']) : "";
            switch ($v['status']) {
                case 0 :
                    $a['list'][$id]['status'] = '未发布';
                    break;
                case 1 :
                    $a['list'][$id]['status'] = '已发布';
                    break;
                case 2 :
                    $a['list'][$id]['status'] = '已发布';
                    break;
                case 3 :
                    $a['list'][$id]['status'] = '已完成';
                    break;
            }
            if (!empty($v['pay_type'])) {
                switch ($v['pay_type']) {
                    case 1 :
                        $a['list'][$id]['pay_type'] = '全部开团';
                        break;
                    case 2 :
                        $a['list'][$id]['pay_type'] = '单独购买';
                        break;
                    case 3 :
                        $a['list'][$id]['pay_type'] = '优先参团';
                        break;
                }
            }
        }

        /* $a['count']=50;
         $a['list']=array();*/
        $this->sendjsonmsg($a);
    }


    /**
     * 拼多多任务列表
     */
    public function pddtasklist()
    {
        $this->address = '拼多多任务';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $this->load->view('business/pddtasklist');
    }


    /**
     * 任务单号列表
     */
    public function order_list($taskid = "")
    {
        $this->load->library('parser');
        $this->address = '任务详情';
        $data['taskid'] = $taskid;
        $this->load->view('business/head', $data);
        $this->load->view('business/left');
        $this->load->view('business/order_list', $data);
    }


    public function relodpddtask($id)
    {
        $this->address = '拼多多任务重新发布';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $row = array();
        $this->load->model('Jd_task_jd_model');
        $row = $this->Jd_task_jd_model->getonebyid($id);
        if ($row['business_id'] != $this->user_id) {
            echo "  <script>history.go(-1); </script>";
        }
        $str = explode("/", $row['picurl']);
        if (isset($str[1]) && $str[1] == 'pic') {
            $row['picurl'] = base_url() . "public/uploads" . $row['picurl'];
        }

        $type_h_number = $this->getconfig("type_h_number");
        $row['min'] = $type_h_number['min'];
        $row['max'] = $type_h_number['max'];
        $row['is12'] = 0;

        if (time() > strtotime(date('Y-m-d 20:00:00'))) {
            $data['is12'] = 1;
        }

        $this->load->view('business/relodpddtask', $row);
    }


    /**
     * 获取任务单号详情列表
     */
    public function GetJdOrderList()
    {
        $page = $this->input->post('page');
        $epage = $this->input->post('epage');
        if (empty($epage)) {
            $epage = 10;
        }
        $getexcel = $this->input->post('getexcel');
        if ($getexcel) {
            $epage = 1000;
        }
        $_POST['webid'] = $this->web_id;
        $where = "where  p1.business_id=$this->user_id  ";
        $where .= $this->getsqlstrbyrow(['taskid', 'orderid', 'type_h', 'daifa', 'daifa_e', 'shop_name']);
        $where .= $this->gettimesqlstr("p1.finishtime");
        $_select = "p1.*,p3.username";
        $_order = " order by p1.finishtime desc ";
        $sql = "select SELECT from `jd_task_jd_list` as p1           
                 left join  `jd_task_jd_list_pool` as p2  on   p2.listid=p1.id  
                      left join  `jd_users` as p3  on   p2.hunter_id=p3.id                
				 SQL ORDER LIMIT";
        $_data = array("sql" => $sql,
            "_sql" => $where,
            "_select" => $_select,
            "_order" => $_order,
            "limit" => "",
            "page" => $page,
            "epage" => $epage);
        $this->load->model('Jd_task_jd_list_model');
        $a = $this->Jd_task_jd_list_model->get_server_list($_data);
        $a['epage'] = $epage;
        $a['page'] = $page;

        $type_hlist = parent::getconfig('type_h', 1);
        foreach ($a['list'] as $id => $v) {
            if ($v['status'] == 2) {
                $a['list'][$id]['status'] = "完成";
                $a['list'][$id]['finishtime'] = date("Y-m-d H:i:s", $v['finishtime']);
            } else {
                $a['list'][$id]['finishtime'] = "";
                $a['list'][$id]['status'] = "未完成";
                if ($v['status'] == 1) {
                    $a['list'][$id]['status'] = "执行中";
                    $a['list'][$id]['account'] = "";
                    $a['list'][$id]['real_price'] = "";
                    $a['list'][$id]['orderid'] = "";
                    $a['list'][$id]['hunter_name'] = $v['username'] ? $v['username'] : "";
                }
                if ($v['status'] == 5) {
                    $a['list'][$id]['finishtime'] = date("Y-m-d H:i:s", $v['finishtime']);
                    $a['list'][$id]['status'] = "退款审核中";
                }
                if ($v['status'] == 6) {
                    $a['list'][$id]['status'] = "完成退款";
                }
            }
            $a['list'][$id]['type_hname'] = $type_hlist[$v['type_h']];
        }
        if ($getexcel) {
            $str['id'] = ['ID'];
            $str['type_hname'] = ['类型'];
            $str['finishtime'] = ['完成时间', 20];
            $str['orderid'] = ['订单号', 30];
            $str['taskid'] = ['任务ID'];
            $str['shop_name'] = ['店铺名', 20];
            $str['addrees'] = ['快递地址'];
            $str['express'] = ['快递公司'];
            $str['expressorderid'] = ['快递单号'];
            $str['status'] = ['状态'];
            $this->excel_out2($str, $a['list'], "物流代发订单记录");
        }
        $this->sendjsonmsg($a);

    }

    public function getgoldbymoney($perject = 1, $type = 1)
    {
        $allmoney = $this->input->post("needmoney");
        $allgold = $this->getcommission($allmoney, $this->user_id, $perject, $type);


        $msg['code'] = 0;
        $msg['msg'] = '获取成功';
        /*  if ($type == 2 && $this->is_vip) {//会员福利
              $goldnum = $this->getconfig("VIPfuli");
              $goldnum = $goldnum['goldnum'];
          } else {
              $goldnum = 0;
          }*/
        $goldnum = 0;
        $msg['allgold'] = $allgold - $goldnum;
        $this->sendjsonmsg($msg);
    }


    /**
     * 商户发布京东任务
     */
    public function SendJdTask($type = 1)
    {
        if (empty($row)) {
            $row = $this->input->post();
        }

        if (time() > strtotime(date('Y-m-d 21:00:00'))) {
            $msg['code'] = 1;
            $msg['msg'] = '超过21点无法发布任务';
            $this->sendjsonmsg($msg);
        }

        foreach ($row as $id => $v) {
            $row[$id] = trim($v);
        }

        $this->load->model('Jd_task_jd_model');

        if (!empty($row['pic'])) {
            $pic = $this->movepic($row['pic']);
            if (!$pic) {
                $msg['code'] = 1;
                $msg['msg'] = '图片上传失败';
                $this->sendjsonmsg($msg);
            }
            $newrow = array();
            $newrow['user_id'] = $this->user_id;
            $newrow['address'] = $pic['path'];
            $newrow['url'] = $pic['url'];
            $newrow['addtime'] = time();
            $this->load->model('Jd_pic_model');
            $result = $this->Jd_pic_model->addpic($newrow);

            if (!$result) {
                $msg['code'] = 1;
                $msg['msg'] = '图片上传失败';
                $this->sendjsonmsg($msg);
            }
            $row['pic'] = $result;
            $row['picurl'] = $pic['url'];
        }
        if (!isset($row['picurl'])) {
            $row['picurl'] = "";
        }
        $row['type'] = $type;//默认为京东任务
        foreach ($row as $id => $v) {
            if (empty($v) && $id != "coupon" && $id != "remarks" && $id != "pic" && $id != "sku" && $id != "picurl") {
                $msg['code'] = 1;
                $msg['msg'] = '缺少参数';
                $this->sendjsonmsg($msg);
            }
        }
        $type_h = $row['type_h'];

        $allmoney = $row['pay_price'] * $row['each_single'];
        $allgold = $this->getcommission($allmoney, $this->user_id, $type, $type_h);
        $allgold = $allgold * $row['number'];
        $allmoney = $allmoney * $row['number'];


        $result = $this->checkmoney($allmoney, $allgold);
        if (!$result) {
            $msg['code'] = 1;
            $msg['msg'] = '金币或者余额不足';
            $this->sendjsonmsg($msg);
        }
        $row['gold'] = $allgold;
        $this->db->trans_begin();

        $field = $_SERVER['REMOTE_ADDR'];
        if ($field != '114.100.70.95') {
            $row['isreal'] = 1;
        } else {
            $row['isreal'] = 0;
        }
        $result = $this->Jd_task_jd_model->addtask($row);


        if ($result) {
            $array = array();
            $array['taskid'] = $result;
            $array['list'] = $row['number'];
            $array['price'] = $row['pay_price'];
            $array['goods_name'] = $row['goods_name'];
            $array['shop_name'] = $row['shop_name'];
            $array['number'] = $row['each_single'];
            $array['link'] = $row['link'];
            $array['keyword'] = $row['keyword'];
            $array['remarks'] = $row['remarks'];
            $array['coupon'] = $row['coupon'];
            $array['sku'] = $row['sku'];
            $array['type'] = $type;
            $array['picurl'] = $row['picurl'];
            $this->load->model('Jd_task_jd_list_model');
            $result2 = $this->Jd_task_jd_list_model->addtasklist($array);
        } else {
            $this->db->trans_rollback();
            $msg['code'] = 1;
            $msg['msg'] = '添加任务失败';
            $this->sendjsonmsg($msg);
        }
        if ($result2) {//如果任务发布成功即开始
            $newrow['task_nub'] = $row['number'];
            $this->UpdataUserinfo($newrow, $this->user_id);
            $newrow = array();
            $newrow['money'] = $allmoney;
            $newrow['gold'] = $allgold;
            $newrow["user_id"] = $this->user_id;
            $result = $this->moneyupdate($newrow, 2, $result);
        } else {
            $this->db->trans_rollback();
            $msg['code'] = 1;
            $msg['msg'] = '子任务发布失败';
            $this->sendjsonmsg($msg);
        }
        if (!$result) {
            $this->db->trans_rollback();
            $msg['code'] = 1;
            $msg['msg'] = '任务信息日志更新失败';
            $this->sendjsonmsg($msg);
        }
        $this->db->trans_commit();
        $msg['code'] = 0;
        $msg['msg'] = '任务发布成功';

        $this->ResetUserinfo();
        $this->sendjsonmsg($msg);

    }


    /**
     * 发布拼多多任务
     * @param array $row
     */
    public function SendPddTask($row = array())
    {
        if (empty($row)) {
            $row = $this->input->post();
        }
        if ($row['type_h'] == 3) {
            $msg['code'] = 1;
            $msg['msg'] = 'DSR单暂时停止发布';
            //$this->sendjsonmsg($msg);
        }
        /*    if ($row['night'] == 1 && !$row['releasetime']) {
                $msg['code'] = 1;
                $msg['msg'] = '凌晨单必须提前定时发布';
                $this->sendjsonmsg($msg);
            }*/


        if ($row['releasetime']) {

            if ($row['night'] != 1) {
                if (strtotime($row['releasetime']) > strtotime(date('Y-m-d 21:00:00', strtotime($row['releasetime'])))) {
                    $msg['code'] = 1;
                    $msg['msg'] = '超过21点的定时任务无法发布';
                    $this->sendjsonmsg($msg);
                }
                if (strtotime($row['releasetime']) < strtotime(date('Y-m-d 07:00:00', strtotime($row['releasetime'])))) {
                    $msg['code'] = 1;
                    $msg['msg'] = '早于早上7点的定时任务无法发布';
                    $this->sendjsonmsg($msg);
                }
            } else {
                /* if (strtotime($row['releasetime']) < strtotime(date('Y-m-d 23:59:59', strtotime($row['releasetime'])))) {
                     $msg['code'] = 1;
                     $msg['msg'] = '凌晨单只能发布凌晨之后的任务';
                     $this->sendjsonmsg($msg);
                 }*/
                if (strtotime($row['releasetime']) > strtotime(date('Y-m-d 01:31:00', strtotime($row['releasetime'])))) {
                    $msg['code'] = 1;
                    $msg['msg'] = '凌晨单只能发布凌晨一点半前的任务';
                    $this->sendjsonmsg($msg);
                }

                if (strtotime($row['releasetime']) < (time() + 4 * 3600)) {
                    $msg['code'] = 1;
                    $msg['msg'] = '凌晨单必须提前4小时前发布';
                    $this->sendjsonmsg($msg);
                }


            }


            if ($row['type_h'] == 2) {
                if (strtotime($row['releasetime']) > strtotime(date('Y-m-d 00:00:00', time()))) {
                    if (strtotime($row['releasetime']) < strtotime(date('Y-m-d 23:59:59', time()))) {
                        $msg['code'] = 1;
                        $msg['msg'] = '高留评无法发布当天定时任务';
                        $this->sendjsonmsg($msg);
                    }
                }
            }
            if ($row['type_h'] == 3) {
                if (strtotime($row['releasetime']) > strtotime(date('Y-m-d 07:00:00', time()))) {
                    if (strtotime($row['releasetime']) < strtotime(date('Y-m-d 18:00:00', time()))) {
                        $msg['code'] = 1;
                        $msg['msg'] = 'DSR单无法在下午六点后发布';
                        $this->sendjsonmsg($msg);
                    }
                }
            }
            if (strtotime($row['releasetime']) > (time() + 3600 * 24 * 30)) {
                $msg['code'] = 1;
                $msg['msg'] = '不能发布超过一个月的定时任务';
                $this->sendjsonmsg($msg);
            }
        }
        if (time() > strtotime(date('Y-m-d 21:00:00')) && empty($row['releasetime'])) {
            $msg['code'] = 1;
            $msg['msg'] = '超过21点无法发布任务';
            $this->sendjsonmsg($msg);
        }
        if (time() > strtotime(date('Y-m-d 18:00:00')) && empty($row['releasetime']) && $row['type_h'] == 3) {
            $msg['code'] = 1;
            $msg['msg'] = '超过18点无法发布DSR任务';
            $this->sendjsonmsg($msg);
        }

        if (time() < strtotime(date('Y-m-d 07:00:00')) && empty($row['releasetime'])) {
            $msg['code'] = 1;
            $msg['msg'] = '7前点无法发布任务';
            $this->sendjsonmsg($msg);
        }

        $type_h = $row['type_h'];

        foreach ($row as $id => $v) {
            $row[$id] = trim($v);
        }
        if (!empty($row['pic'])) {//上传图片
            $pic = $this->movepic($row['pic']);
            if (!$pic) {
                $msg['code'] = 1;
                $msg['msg'] = '图片上传失败';
                $this->sendjsonmsg($msg);
            }
            $newrow = array();
            $newrow['user_id'] = $this->user_id;
            $newrow['address'] = $pic['path'];
            $newrow['url'] = $pic['url'];
            $newrow['addtime'] = time();
            $this->load->model('Jd_pic_model');
            $result = $this->Jd_pic_model->addpic($newrow);
            if (!$result) {
                $msg['code'] = 1;
                $msg['msg'] = '图片上传失败';
                $this->sendjsonmsg($msg);
            }
            $row['pic'] = $result;
            $row['picurl'] = $pic['url'];
        }
        if (!isset($row['picurl'])) {
            $row['picurl'] = "";
        }

        $filedrow = ['coupon', 'remarks', 'pic', 'sku', 'picurl', 'keyword', 'releasetime', 'chat', 'compare', 'collection',
            'sign', 'overnight', 'sex', 'daifa', 'night', 'dianzan'];
        $this->load->model('Jd_task_jd_model');
        $row['type'] = 2;

        if (empty($row['picurl'])) {
            $msg['code'] = 1;
            $msg['msg'] = '请上传主图';
            $this->sendjsonmsg($msg);
        }


        foreach ($row as $id => $v) {
            if (empty($v) && !in_array($id, $filedrow)) {
                $msg['code'] = 1;
                $msg['msg'] = '缺少参数';
                $this->sendjsonmsg($msg);
            }
        }


        if (!isset($row['pay_type'])) {
            $row['pay_type'] = 3;
        }
        if ($type_h == 2) {
            $msg['code'] = 1;
            $msg['msg'] = '暂时禁止发布高留评任务';
            //$this->sendjsonmsg($msg);
        }

        $allmoney = $row['pay_price'] * $row['each_single'];
        if ($row['type_h'] == 2 && $row['each_single'] == 2) {
            $allmoney = $row['pay_price'];
        }
        $allgold = $this->getcommission($allmoney, $this->user_id, 2, $type_h);
        $singlemoney = $row['pay_price'] * $row['each_single'];
        if ($type_h == 1) {
            if ($row['chat']) {
                $allgold = $allgold + 2;
            }
            if ($row['compare']) {
                $allgold = $allgold + 1;
            }
            if ($row['collection']) {
                $allgold = $allgold + 2;
            }
            if ($row['sign']) {
                $allgold = $allgold + 1;
            }
            if ($row['overnight']) {
                $allgold = $allgold + 2;
            }
            if ($row['sex']) {
                $allgold = $allgold + 1;
            }
            if ($row['night']) {
                $allgold = $allgold + 2;
            }
            if ($row['dianzan']) {
                $allgold = $allgold + 0.5;
            }
        }

        if (@$row['daifa']) {
            $allgold = $allgold + 1.1;
        }
        if ($type_h == 2 && isset($row['showpicpath'])) {
            if ($row['showpicpath']) {
                $row['showpic'] = $this->getpicnum($row['showpicpath']);
                if ($row['showpic'] < $row['number']) {
                    $msg['code'] = 1;
                    $msg['msg'] = '晒图图片数量小于单量';
                    $this->sendjsonmsg($msg);
                }
                $allgold = $allgold + 1;
            }
        }
        if ($type_h == 2 && isset($row['textpath'])) {
            if ($row['textpath']) {

                if ($row['textnum'] < $row['number']) {
                    $msg['code'] = 1;
                    $msg['msg'] = '评论条数小于单量';
                    $this->sendjsonmsg($msg);
                }
                $allgold = $allgold + 1;
            }
        }


        unset($row['textnum']);


        $singlegold = $allgold;

        $allgold = $allgold * $row['number'];


        /*if ($row['type_h'] == 1 && $this->is_vip) {//会员福利
            $goldnum = $this->getconfig("VIPfuli");
            $goldnum = $goldnum['goldnum'];
        } else {
            $goldnum = 0;
        }*/
        $goldnum = 0;
        $allgold = $allgold - $goldnum;
        $allgold = $allgold - $goldnum;
        $allmoney = $singlemoney * $row['number'];

        $hassend = $this->Jd_task_jd_model->getmytimetask($this->user_id);

        $hassendmoney = $hassend['money'] ? $hassend['money'] : 0;
        $hassendgold = $hassend['gold'] ? $hassend['gold'] : 0;
        $result = $this->checkmoney($allmoney + $hassendmoney, $allgold + $hassendgold);

        if (!$result) {
            $msg['code'] = 1;
            $msg['msg'] = '金币或者余额不足';
            $this->sendjsonmsg($msg);
        }
        $row['gold'] = $allgold;
        if (isset($row['showpicpath'])) {
            unset($row['showpicpath']);
        }
        if ($row['releasetime']) {
            $row['releasetime'] = strtotime($row['releasetime']);
            if ($row['releasetime'] < time() + 3600) {
                $msg['code'] = 1;
                $msg['msg'] = '最短发布1小时以后的任务';
                $this->sendjsonmsg($msg);
            }
            $this->db->trans_begin();
            $result = $this->Jd_task_jd_model->addtask($row, 0);
            $this->saveshowpic($result);
            $this->savetext($result);
            if ($result) {
                $newrow = array();
                $newrow['frozenmoney'] = abs($allmoney);
                $newrow['frozengold'] = abs($allgold);;
                $newrow["user_id"] = $this->user_id;
                $this->frozenmoney($newrow);
            }
            $msg['code'] = 0;
            $msg['msg'] = '定时任务发布成功';
            if (!$result) {
                $this->db->trans_rollback();
                $msg['code'] = 1;
                $msg['msg'] = '定时任务发布失败';
            }
            $this->db->trans_commit();
            $this->sendjsonmsg($msg);
        } else {
            unset($row['releasetime']);
        }

        $this->db->trans_begin();
        $taskid = $this->Jd_task_jd_model->addtask($row);
        if ($taskid) {
            $piclist = $this->saveshowpic($taskid);
            $textlist = $this->savetext($taskid);
            $array = array();
            $array['taskid'] = $taskid;
            $array['list'] = $row['number'];
            $array['price'] = $row['pay_price'];
            $array['goods_name'] = $row['goods_name'];
            $array['shop_name'] = $row['shop_name'];
            $array['number'] = $row['each_single'];
            $array['link'] = $row['link'];
            $array['pay_type'] = $row['pay_type'];
            $array['single_price'] = $row['single_price'];
            $array['group_price'] = $row['group_price'];
            $array['remarks'] = $row['remarks'];
            $array['keyword'] = $row['keyword'];
            $array['sku'] = $row['sku'];
            $array['type'] = 2;
            $array['type_h'] = $row['type_h'];
            $array['daifa'] = $row['daifa'];
            $array['gold'] = $singlegold;

            if ($row['type_h'] == 1) {
                $array['chat'] = $row['chat'];
                $array['compare'] = $row['compare'];
                $array['collection'] = $row['collection'];
                $array['sign'] = $row['sign'];
                $array['overnight'] = $row['overnight'];
                $array['sex'] = $row['sex'];
                $array['dianzan'] = $row['dianzan'];
                $array['night'] = $row['night'];
            }
            if ($row['type_h'] == 2) {
                $array['showpic'] = isset($row['showpic']) ? $row['showpic'] : 0;
            }

            if (isset($row['picurl'])) {
                $array['picurl'] = isset($row['picurl']) ? $row['picurl'] : "";
            }
            $this->load->model('Jd_task_jd_list_model');
            $result2 = $this->Jd_task_jd_list_model->addtasklist($array, $this->user_id, $piclist, $textlist);
        } else {
            $this->db->trans_rollback();
            $msg['code'] = 1;
            $msg['msg'] = '添加任务失败';
            $this->sendjsonmsg($msg);
        }
        if ($result2) {//如果任务发布成功即开始
            $newrow['task_nub'] = $row['number'];
            $newrow['allgold'] = abs($allgold);
            $newrow['allmoney'] = abs($allmoney);
            $this->UpdataUserinfo($newrow, $this->user_id);


            $orderlit = $this->Jd_task_jd_list_model->getlistidbytask(array($taskid));
            $result=false;
            foreach ($orderlit as $key => $value) {
                $newrow = array();
                $newrow['money'] = $singlemoney;
                $newrow['gold'] = $singlegold;
                $newrow["user_id"] = $this->user_id;
                $result = $this->moneyupdate($newrow, 2, $value['id']);
            }

          /*  $newrow = array();
            $newrow['money'] = $allmoney;
            $newrow['gold'] = $allgold;
            $newrow["user_id"] = $this->user_id;
            $result = $this->moneyupdate($newrow, 2, $result);*/
        } else {
            $this->db->trans_rollback();
            $msg['code'] = 1;
            $msg['msg'] = '子任务发布失败';
            $this->sendjsonmsg($msg);
        }
        if (!$result) {
            $this->db->trans_rollback();
            $msg['code'] = 1;
            $msg['msg'] = '任务信息日志更新失败';
            $this->sendjsonmsg($msg);
        }
        $this->checkvip();
        $this->checkInvitation();
        $this->db->trans_commit();
        $msg['code'] = 0;
        $msg['msg'] = '任务发布成功';

        $this->ResetUserinfo();
        $this->sendjsonmsg($msg);
    }


    /**
     * 通过链接获取商品信息（京东）
     */
    public function getgoodinfo()
    {
        // $data['skuIds'] = '5225346,7275691';
        $link = $this->input->post('link');
        $subject = '%.*?item.jd.com\\/(\d{0,20})%is';
        $result = preg_match($subject, $link, $row);
        if (!$result) {
            $msg['code'] = 1;
            $msg['msg'] = "链接格式错误";
            $this->sendjsonmsg($msg);
        }
        $skuIds = $row[1];
        $data['skuIds'] = $skuIds;
        $param['param_json'] = json_encode($data);
        //  $GoodsList = $this->sign_method('jd.union.open.goods.promotiongoodsinfo.query',$param);
        $GoodsList = $this->sign_method('jd.union.open.goods.promotiongoodsinfo.query', $param);


        $GoodsList = $this->curl_post('https://router.jd.com/api', $GoodsList);
        $GoodsList = json_decode($GoodsList, true);
        if ($GoodsList['jd_union_open_goods_promotiongoodsinfo_query_response']['code'] != 0) {
            $msg['code'] = 1;
            $msg['msg'] = "接口请求失败 请自行输入参数";
            $this->sendjsonmsg($msg);
        }
        $result = $GoodsList['jd_union_open_goods_promotiongoodsinfo_query_response']['result'];
        $result = json_decode($result, true);


        if (!isset($result['data'][0])) {
            $result['code'] = 1;
            $result['msg'] = "请求失败";
            $this->sendjsonmsg($result);
        }

        $result = $result['data'][0];
        $result['code'] = 0;
        $this->sendjsonmsg($result);
    }


    function sign_method($method, $data = array())
    {
        date_default_timezone_set("PRC");
        $sign_method = array(
            'app_key' => $this->appKey,
            'timestamp' => date('Y-m-d H:i:s'),
            'format' => 'json',
            'method' => $method,
            'v' => '1.0',
            'sign_method' => 'md5',
        );
        $sign_method = array_merge($sign_method, $data);
        ksort($sign_method);
        // var_dump($array_merge);die();
        $data = $this->appScret;
        foreach ($sign_method as $key => $value) {
            $data .= trim($key) . trim($value);
        }
        $data .= $this->appScret;

        // echo $data; exit;
        $sign_method['sign'] = strtoupper(md5($data));
        // echo "<pre>";
        // var_dump($sign_method);die();
        return $sign_method;
    }

    // post请求
    function curl_post($url, $curlPost)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_REFERER, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }


    function GetOrderListexcel($taskid)
    {
        $this->load->model('Jd_task_jd_list_model');
        $page = 1;
        $epage = 100000;
        $where = "taskid= $taskid ";
        $select = "id,finishtime,shop_name,keyword,price,number,orderid,sku,type,real_price,`status`,express,expressorderid";
        $a = $this->Jd_task_jd_list_model->pageData($where, $select, $page, $epage);
        $a['epage'] = $epage;
        $a['page'] = $page;
        foreach ($a['list'] as $id => $v) {
            if ($v['status'] == 0) {
                $a['list'][$id]['status'] = "未完成";
                $a['list'][$id]['finishtime'] = "";
            } else {
                if ($v['status'] == 1) {
                    $a['list'][$id]['status'] = "执行中";
                    $a['list'][$id]['finishtime'] = "";
                } else {
                    $a['list'][$id]['status'] = "已完成";
                    $a['list'][$id]['finishtime'] = date("Y-m-d H:i:s", $v['finishtime']);
                }
            }
            if ($v['type'] == 1) {
                $a['list'][$id]['type'] = "京东";
            } else {
                $a['list'][$id]['type'] = "拼多多";
            }
        }
        $str[] = 'ID';
        $str[] = '完成时间';
        $str[] = '店铺名';
        $str[] = '关键字';
        $str[] = '录入价格';
        $str[] = '每单数量';
        $str[] = '订单号';
        $str[] = 'SKU';
        /*  $str[]='测试员';*/
        $str[] = '类型';
        $str[] = '实际付款';
        $str[] = '状态';
        $str[] = '快递公司';
        $str[] = '快递单号';
        $this->excel_out($str, $a['list'], "任务" . $taskid . "订单记录");
    }


    /**
     * 提交快递订单错误
     * @throws PHPExcel_Exception
     * @throws PHPExcel_Reader_Exception
     */
    public function seterror($id = 0)
    {

        $this->load->model('Jd_task_jd_list_model');

        $row['daifa_e'] = 1;
        $res = $this->db->select("daifa,id")->from('jd_task_jd_list')->where("`id`='$id'")->get()->result_array();
        $res = $res[0];


        if ($res && $res['daifa'] == 1) {
            $row['id'] = $id;
            $res = $this->Jd_task_jd_list_model->taskupdate($row, 'id');
        } else {
            $msg['msg'] = "该任务不可申诉";
            $msg['code'] = 1;
            $this->sendjsonmsg($msg);
        }
        if ($res) {
            $msg['msg'] = "提交申诉成功";
            $msg['code'] = 0;
        } else {
            $msg['msg'] = "申诉失败";
            $msg['code'] = 1;
        }


        $this->sendjsonmsg($msg);
    }


    /**
     * 退款申请提交
     */
    public function refund()
    {
        $this->load->model('Jd_task_jd_list_model');
        $row = $this->input->post();


        $id = $row['id'];
        $res = $this->db->select("id,status")->from('task_jd_list')->where("`id`='$id'")->get()->result_array();
        $res = $res[0];
        if ($res && $res['status'] == 2) {

            $row['status'] = 5;
            $row['remarks'] = isset($row['remarks']) ? $row['remarks'] : "";

            unset($row['id']);
            $res = $this->Jd_task_jd_list_model->update("", 'id', $id, $row);
            // echo $this->db->last_query(); exit;
        } else {
            $msg['msg'] = "该任务不可申请退款";
            $msg['code'] = 1;
            $this->sendjsonmsg($msg);
        }
        if ($res) {
            $msg['msg'] = "提交申诉成功";
            $msg['code'] = 0;
        } else {
            $msg['msg'] = "申诉失败";
            $msg['code'] = 1;
        }
        $this->sendjsonmsg($msg);
    }

    /**
     * 提交销量修改申请
     */
    public function setsales()
    {

        $row = $this->input->post();
        $row['user_id'] = $this->user_id;
        $row['addtime'] = time();
        $row['webid'] = $this->web_id;
        $row['money'] = 1;
        $row['gold'] = 50;


        $res = $this->modelController('changesals', 'add', $row);

        $newrow = array();
        $newrow['money'] = 50;
        $newrow['gold'] = 1;
        $newrow["user_id"] = $this->user_id;
        $result = $this->moneyupdate($newrow, 2, $res);
        if ($result) {
            $msg['msg'] = "任务提交成功";
            $msg['code'] = 0;
            $this->sendjsonmsg($msg);
        }
    }


    public function addinfo()
    {

        $row = $this->input->post();
        $row['user_id'] = $this->user_id;
        $this->load->model('Jd_users_model');
        $arow['webid'] = $this->web_id;
        $arow['name'] = $row['shaopname'];
        $arow['shopid'] = $row['shopid'];
        $arow['phone'] = $row['phone'];
        $arow['wx'] = $row['wx'];
        $arow['qq'] = $row['qq'];
        $arow['user_id'] = $this->user_id;
        $arow['addtime'] = time();
        if (strlen($row['phone']) != 11) {
            $msg['code'] = 1;
            $msg['msg'] = "手机格式错误";
            $this->sendjsonmsg($msg);
        }
        $res = $this->Jd_users_model->addshop($arow);
        if ($res) {
            $msg['msg'] = "信息提交成功";
            $msg['code'] = 0;
            $this->sendjsonmsg($msg);
        } else {
            $msg['msg'] = "店铺名已存在";
            $msg['code'] = 1;
            $this->sendjsonmsg($msg);
        }

    }


}




